
package com.sskj.jkzx.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sskj.jkzx.domain.entity.EngQueryParam;
import com.sskj.jkzx.domain.entity.eng.EngTsInfo;
import com.sskj.jkzx.vo.RestParamBean;
import com.sskj.jkzx.vo.RestQueryBean;
import com.sskj.utils.HttpHelp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "**/sskj/ts")
@Api(value = "图审信息查询", description = "图审信息查询")
public class TsInfoServiceImpl {

	protected final Log logger = LogFactory.getLog(TsInfoServiceImpl.class);

	@Value("${sskj.tsServer}")
	private String tsServer;

	/***
	 * 图审信息
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/ts", method = RequestMethod.GET)
	@ApiOperation(value = "图审信息查询", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sgzbName", value = "施工标段名称", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "jlzbName", value = "监理标段名称", paramType = "form", dataType = "string") })
	public List<EngTsInfo> queryEngTsInfo(@ApiIgnore EngQueryParam engQueryParam) {

		List<EngTsInfo> results = new ArrayList<EngTsInfo>();

		RestQueryBean queryBean = new RestQueryBean();
		queryBean.setSrvName("cmis");
		queryBean.setSrvPwd("cmis");

		RestParamBean paramBean = new RestParamBean();
		if (StringUtils.isNotEmpty(engQueryParam.getSgzbName())) {
			queryBean.getParams().clear();
			paramBean.setPn("name");
			paramBean.setSv(engQueryParam.getSgzbName());
			queryBean.getParams().add(paramBean);

			try {
				String stss = HttpHelp.sendPost(tsServer + "/seam/resource/v1/sgts/jzgc/gongshi/eng/gcmc",
						JSONObject.toJSONString(queryBean));

				if (stss != null) {
					results.addAll(JSON.parseArray(stss, EngTsInfo.class));
				}
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}

		if (StringUtils.isNotEmpty(engQueryParam.getJlzbName())) {
			queryBean.getParams().clear();
			paramBean.setPn("name");
			paramBean.setSv(engQueryParam.getJlzbName());
			queryBean.getParams().add(paramBean);

			try {
				String jtss = HttpHelp.sendPost(tsServer + "/seam/resource/v1/sgts/jzgc/gongshi/eng/gcmc",
						JSONObject.toJSONString(queryBean));
				if (jtss != null && !results.containsAll(JSON.parseArray(jtss, EngTsInfo.class))) {
					results.addAll(JSON.parseArray(jtss, EngTsInfo.class));
				}
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

}
